package com.srd.phone.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.shengrongdai.srd.R;
import com.srd.phone.activity.FundManageActivity;
import com.srd.phone.activity.InvestManageActivity;
import com.srd.phone.activity.InviteFriendsActivity;
import com.srd.phone.activity.LoanManageActivity;
import com.srd.phone.activity.MessagePublishActivity;
import com.srd.phone.activity.MyAwardActivity;
import com.srd.phone.activity.MyMessageActivity;
import com.srd.phone.activity.ObligationsManageActivity;
import com.srd.phone.activity.RechargeActivity;
import com.srd.phone.activity.SafetyManageActivity;
import com.srd.phone.activity.UserManageActivity;
import com.srd.phone.activity.WithdrawActivity;
import com.srd.phone.bean.TabMine;
import com.srd.phone.bean.UserInfo;
import com.srd.phone.mvp.presenters.UtilPresenter;
import com.srd.phone.utils.Constants;
import com.srd.phone.utils.UserUtils;
import com.srd.phone.utils.views.CircleImageView;

/**
 * 我的
 */
public class TabMineFragment extends BaseFragment implements View.OnClickListener {

    private View rootView;
    private TextView tv_common_right, tv_username, tv_ranking, tv_balance, tv_fund_frost, tv_count_recharge,
            tv_count_invest, tv_count_earnings, tv_coupon, tv_interest;
    private CircleImageView iv_head;
    private UtilPresenter utilPresenter = null;
    private UserInfo userInfo = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_tab_mine, container, false);
            initView();
            initData();
        }
        return rootView;
    }

    @Override
    public void initView() {
        (rootView.findViewById(R.id.iv_common_back)).setVisibility(View.GONE);
        ((TextView) rootView.findViewById(R.id.tv_common_title)).setText("我的");
        tv_common_right = (TextView) rootView.findViewById(R.id.tv_common_right);
        tv_common_right.setText("信息披露");
        tv_common_right.setTextColor(Color.BLACK);
        tv_common_right.setOnClickListener(this);
        tv_common_right.setVisibility(View.VISIBLE);

        iv_head = (CircleImageView) rootView.findViewById(R.id.iv_head);
        tv_username = (TextView) rootView.findViewById(R.id.tv_username); // 姓名
        tv_ranking = (TextView) rootView.findViewById(R.id.tv_ranking); // 排名
        tv_balance = (TextView) rootView.findViewById(R.id.tv_balance); // 账户余额
        tv_fund_frost = (TextView) rootView.findViewById(R.id.tv_fund_frost); // 冻结金额
        tv_count_recharge = (TextView) rootView.findViewById(R.id.tv_count_recharge);
        tv_count_invest = (TextView) rootView.findViewById(R.id.tv_count_invest);
        tv_count_earnings = (TextView) rootView.findViewById(R.id.tv_count_earnings);
        tv_coupon = (TextView) rootView.findViewById(R.id.tv_coupon); // 本金
        tv_interest = (TextView) rootView.findViewById(R.id.tv_interest); // 利息

        rootView.findViewById(R.id.ll_user_manage).setOnClickListener(this);
        rootView.findViewById(R.id.ll_fund_manage).setOnClickListener(this);
        rootView.findViewById(R.id.ll_obligations_manage).setOnClickListener(this);
        rootView.findViewById(R.id.ll_invest_manage).setOnClickListener(this);
        rootView.findViewById(R.id.ll_borrow_money).setOnClickListener(this);
        rootView.findViewById(R.id.ll_invite_friend).setOnClickListener(this);
        rootView.findViewById(R.id.ll_my_award).setOnClickListener(this);
        rootView.findViewById(R.id.ll_safety_manage).setOnClickListener(this);
        rootView.findViewById(R.id.ll_my_message).setOnClickListener(this);
        rootView.findViewById(R.id.tv_withdraw).setOnClickListener(this);
        rootView.findViewById(R.id.tv_recharge).setOnClickListener(this);
    }

    @Override
    public void initData() {
        refreshView();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
//        if (isVisibleToUser && !mContext.getLoginStatus()) {
//            startActivity(new Intent(mContext, LoginActivity.class));
//        }
    }

    private void refreshView() {
        userInfo = UserUtils.getUserInfo();
        tv_username.setText(userInfo.userName);
        tv_ranking.setText("平台排名：" + userInfo.order + "名");
        // 账户余额
        tv_balance.setText("账户余额：" + userInfo.total + "元");
        // 冻结
        tv_fund_frost.setText("冻结资金：" + userInfo.frozenfund + "元");
        // 累积充值
        tv_count_recharge.setText(userInfo.scRecord + "元");
        // 累积投资
        tv_count_invest.setText(userInfo.sinves + "元");
        // 累积收益
        tv_count_earnings.setText(userInfo.sprofit + "元");
        // 本金
        tv_coupon.setText("本金：" + userInfo.sumreco + "元");
        //  利息
        tv_interest.setText("利息：" + userInfo.penmoney + "元");
    }

    private void getData() {
        utilPresenter.noParams(Constants.OVERVIEWS);
    }

    @Override
    public void returnData(String tag, String data) {
        super.returnData(tag, data);
        if (Constants.OVERVIEWS.equals(tag)) {
            UserUtils.saveOverViews(TabMine.parse(data));
            refreshView();
        }
    }

    @Override
    public void onError(String tag, VolleyError error) {
        super.onError(tag, error);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_common_right: // 信息披露
                gotoActivity(MessagePublishActivity.class);
                break;
            case R.id.tv_withdraw:
                gotoActivity(WithdrawActivity.class);  // 提现
                break;
            case R.id.tv_recharge:
                gotoActivity(RechargeActivity.class);  // 充值
                break;
            case R.id.ll_user_manage: // 用户管理
                gotoActivity(UserManageActivity.class);
                break;
            case R.id.ll_fund_manage: // 资金管理
                gotoActivity(FundManageActivity.class);
                break;
            case R.id.ll_obligations_manage:  // 债权管理
                gotoActivity(ObligationsManageActivity.class);
                break;
            case R.id.ll_invest_manage:  // 投资管理
                gotoActivity(InvestManageActivity.class);
                break;
            case R.id.ll_borrow_money:  // 借款管理
                gotoActivity(LoanManageActivity.class);
                break;
            case R.id.ll_invite_friend:  // 邀请好友
                gotoActivity(InviteFriendsActivity.class);
                break;
            case R.id.ll_my_award:     // 我的奖励
                gotoActivity(MyAwardActivity.class);
                break;
            case R.id.ll_safety_manage:   // 安全管理
                gotoActivity(SafetyManageActivity.class);
                break;
            case R.id.ll_my_message:    // 我的消息
                gotoActivity(MyMessageActivity.class);
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        userInfo = UserUtils.getUserInfo();
        ImageLoader.getInstance().displayImage(Constants.BASE_HEAD_URL + userInfo.uid + "/" + userInfo.src, iv_head);

        presenter = new UtilPresenter(requestQueue, this);
        utilPresenter = (UtilPresenter) presenter;
        setUrlArrs(Constants.OVERVIEWS);
        getData();
    }
}
